#include <iostream>
#include <set>
#include <algorithm>
#define maxa 1000004
using namespace std;

using LL = long long;

const LL INF = 1e18l;

inline void try_erase(multiset<LL>& s, const LL& x)
{
    auto it = s.find(x);
    if(it != s.end()) s.erase(it);
}

LL f1[maxa], f2[maxa];

int main()
{
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int T;
    cin >> T;
    while(T--)
    {
        int n;
        cin >> n;
        for(int i=1; i<maxa; i++)
            f1[i] = f2[i] = -INF;
        f1[0] = f2[0] = 0LL;
        multiset<LL> s1, s2;
        s1.insert(0LL), s2.insert(0LL);
        int lst = 0;
        LL dt = 0LL;
        while(n--)
        {
            int x;
            cin >> x;
            if(x == lst) dt += x;
            else
            {
                LL p = max(f2[x] + x, *s2.rbegin()),
                   q = max(f1[x] + x, *s1.rbegin());
                if(p > f1[lst])
                {
                    try_erase(s1, f1[lst]);
                    s1.insert(f1[lst] = p);
                }
                if(q > f2[lst])
                {
                    try_erase(s2, f2[lst]);
                    s2.insert(f2[lst] = q);
                }
            }
            lst = x;
        }
        cout << max(*s1.rbegin(), *s2.rbegin()) + dt << endl;
    }
    return 0;
}